package com.wyj.wuliwuli.service.impl;

import com.wyj.wuliwuli.common.AjaxRequest;
import com.wyj.wuliwuli.common.AjaxResult;
import com.wyj.wuliwuli.common.PageParam;
import com.wyj.wuliwuli.common.ResultFactory;
import com.wyj.wuliwuli.common.CommonValue;
import com.wyj.wuliwuli.entity.WuliUser;
import com.wyj.wuliwuli.entity.mapper.WuliUserMapper;
import com.wyj.wuliwuli.service.WuliUserService;
import com.wyj.wuliwuli.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @Author wyj
 * @Date 2022.2.14 21:19
 * @Version 1.0
 * @Description
 */

@RequiredArgsConstructor
@Service
public class WuliUserServiceImpl implements WuliUserService {

    private final WuliUserMapper wuliUserMapper;

    @Override
    public boolean isExist(String username) {
        WuliUser wuliUser = wuliUserMapper.findByUsername(username);
        if(wuliUser != null) {
            return true;
        }
        return false;
    }

    @Override
    public AjaxResult getById(Long id) {
        WuliUser wuliUser = wuliUserMapper.findById(id).get();
        AjaxResult result = ResultFactory.buildSuccessResult();
        result.setData(wuliUser);
        return result;
    }

    @Override
    public AjaxResult register(AjaxRequest<WuliUser> ajaxRequest) {
        WuliUser wuliUser = ajaxRequest.getData();
        if(StringUtils.isBlank(wuliUser.getUsername()) || StringUtils.isBlank(wuliUser.getPassword())) {
            return ResultFactory.buildFailResult("用户名或密码不能为空");
        }
        if(wuliUser.getId() == null && this.isExist(wuliUser.getUsername())) {
            return ResultFactory.buildFailResult("用户名已存在");
        }
        if(StringUtils.isBlank(wuliUser.getName())) {
            return ResultFactory.buildFailResult("姓名不能为空");
        }
        if(StringUtils.isBlank(wuliUser.getRole())) {
            return ResultFactory.buildFailResult("角色不能为空");
        }
        String salt = StringUtils.getRandomString(5);
        String password = wuliUser.getPassword() + salt;
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        wuliUser.setSalt(salt);
        wuliUser.setPassword(password);
        wuliUserMapper.save(wuliUser);
        return ResultFactory.buildSuccessResult();
    }




    @Override
    public AjaxResult update(AjaxRequest<WuliUser> ajaxRequest) {
        WuliUser wuliUser = ajaxRequest.getData();
        if(StringUtils.isBlank(wuliUser.getUsername())) {
            return ResultFactory.buildFailResult("用户名不能为空");
        }
        if(wuliUser.getId() == null) {
            return ResultFactory.buildFailResult("id不能为空");
        }
        if(StringUtils.isBlank(wuliUser.getName())) {
            return ResultFactory.buildFailResult("姓名不能为空");
        }
        if(StringUtils.isBlank(wuliUser.getRole())) {
            return ResultFactory.buildFailResult("角色不能为空");
        }
        WuliUser user = wuliUserMapper.getById(wuliUser.getId());
        user.setName(wuliUser.getName());
        user.setGender(wuliUser.getGender());
        user.setEmail(wuliUser.getEmail());
        user.setPhone(wuliUser.getPhone());
        user.setRemark(wuliUser.getRemark());
        user.setAvatar(wuliUser.getAvatar());
        wuliUserMapper.save(user);
        return AjaxResult.of();
    }

    @Override
    public AjaxResult page(AjaxRequest<PageParam> ajaxRequest) {
        PageParam pageParam = ajaxRequest.getData();
        if(ObjectUtils.isEmpty(pageParam)) {
            return ResultFactory.buildFailResult("参数不能为空");
        }

        AjaxResult result = ResultFactory.buildSuccessResult();
        Page<WuliUser> page;
        // 根据是否有筛选字段调用不同的分页方法
        if(null != ajaxRequest.getParams() && StringUtils.isNotBlank(ajaxRequest.getParams().getString(CommonValue.Property.keyword))) {
            // 如果有关键字，那么构建关键字的匹配条件
            WuliUser wuliUser = new WuliUser();
            wuliUser.setUsername(ajaxRequest.getParams().getString(CommonValue.Property.keyword));
            ExampleMatcher matcher = ExampleMatcher.matching().withMatcher(CommonValue.Property.username, ExampleMatcher.GenericPropertyMatcher::contains);
            Example<WuliUser> example = Example.of(wuliUser, matcher);
            page = wuliUserMapper.findAll(example, PageRequest.of(pageParam.getPageNum(), pageParam.getPageSize(), pageParam.getDirection(), CommonValue.Property.updateDateTime));
        } else {
            page = wuliUserMapper.findAll(PageRequest.of(pageParam.getPageNum(), pageParam.getPageSize(), pageParam.getDirection(), CommonValue.Property.updateDateTime));
        }
        result.setData(page);
        return result;
    }

    @Override
    public AjaxResult list() {
        List<WuliUser> wuliUserList = wuliUserMapper.findAll();
        AjaxResult result = ResultFactory.buildSuccessResult();
        result.setData(wuliUserList);
        return result;
    }

    @Override
    public AjaxResult password(AjaxRequest<WuliUser> ajaxRequest) {
        WuliUser user = ajaxRequest.getData();
        if(null == user || null == user.getId() || StringUtils.isBlank(user.getPassword())) {
            return ResultFactory.buildFailResult("参数错误");
        }
        WuliUser wuliUser = wuliUserMapper.getById(user.getId());
        String salt = StringUtils.getRandomString(5);
        String password = user.getPassword() + salt;
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        wuliUser.setPassword(password);
        wuliUser.setSalt(salt);
        wuliUserMapper.save(wuliUser);
        return ResultFactory.buildSuccessResult();
    }

    @Override
    public WuliUser getByUsername(String username) {
        return wuliUserMapper.findByUsername(username);
    }

    @Override
    public AjaxResult delete(AjaxRequest<Object> ajaxRequest) {
        if(null == ajaxRequest.getParams() || null == ajaxRequest.getParams().getLong(CommonValue.Property.id)) {
            return ResultFactory.buildFailResult("参数不能为空");
        }
        wuliUserMapper.deleteById(ajaxRequest.getParams().getLong(CommonValue.Property.id));
        return ResultFactory.buildSuccessResult();
    }
}
